package com.thti.mecool.client.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具
 * 
 * @author weiguo.ren
 * 
 */
public class TimeUtil {

	/**
	 * 获取当前的毫秒时间
	 * 
	 * @return
	 */
	public static long getDate() {
		Date date = new Date();
		return date.getTime();
	}

	public static String getDateTime() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String date = sDateFormat.format(new java.util.Date());
		return date;
	}

	/**
	 * mode==1时 判断是否在当前日期之前 mode==2时 判断是否在当前日期之后 mode==3时 判断日期是否与当天日期相等 mode==4时
	 * 判断是否与当前时间相等 精确到分
	 * 
	 * @param mode
	 * @return
	 */
	public static boolean judgeCurDate(int mode, Date date) {
		Date curDate = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy" + "MM" + "dd");
		switch (mode) {
		case 1:
			return Integer.parseInt(sdf3.format(curDate)) >= Integer
					.parseInt(sdf3.format(date));
		case 2:
			return Integer.parseInt(sdf3.format(curDate)) < Integer
					.parseInt(sdf3.format(date));
		case 3:
			return Integer.parseInt(sdf3.format(date)) == Integer.parseInt(sdf3
					.format(curDate));
		case 4:
			return date.getTime() / 60 / 1000 == curDate.getTime() / 60 / 1000;
		}
		return false;
	}
	public static boolean judgeCurDate(String date) {
		Long curDate = System.currentTimeMillis();
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Long lDate=sdf3.parse(date).getTime();
			if(lDate>curDate){
				return true;
			}else{
				return false;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static boolean judgeCurTime(int mode, String time1, String time2) {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = sdf2.parse(time1);
			date2 = sdf2.parse(time2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		switch (mode) {
		case 1:
			return Integer.parseInt(sdf.format(date1)) > Integer.parseInt(sdf
					.format(date2));
		}
		return false;
	}

	public static String DateConvert(String d, int flag) {
		String date = null;
		SimpleDateFormat sdf;
		SimpleDateFormat sdf2;
		switch (flag) {
		/**
		 * 日期
		 */
		case 1:
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
			try {
				date = sdf.format(sdf2.parse(d));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			break;
		/**
		 * 时间
		 */
		case 2:
			sdf = new SimpleDateFormat("HH:mm:ss");
			sdf2 = new SimpleDateFormat("HH:mm");
			try {
				date = sdf.format(sdf2.parse(d));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			break;
		case 3:
			sdf = new SimpleDateFormat("HH:mm");
			try {
				date = sdf.format(sdf.parse(d));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			break;
		case 4:
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = sdf.format(sdf2.parse(d));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			break;

		case 5:
			sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = sdf.format(sdf2.parse(d));
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			break;
			
		}
		return date;
		}


	

	/**
	 * c 时间转换
	 * 
	 * @param d
	 * @param flag
	 *            返回时间类型 0 返回yyyy-MM-dd HH:mm:ss 1 返回yyyy年MM月dd HH:mm:ss 2
	 *            yyyy年MM月dd 3 返回yyyy-MM-dd
	 * @return
	 */
	public static String getDateConvert(String d, int flag) {
		Long time = new Long(d);
		SimpleDateFormat sDateFormat;
		switch (flag) {
		case 0:
			sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		case 1:
			sDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
			break;
		case 2:
			sDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
			break;
		case 3:
			sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			break;
		case 4:
			sDateFormat = new SimpleDateFormat("MM月dd日");
			break;
		case 5:
			sDateFormat = new SimpleDateFormat("yyyy");
			break;
		case 6:
			sDateFormat = new SimpleDateFormat("MM");
			break;
		case 7:
			sDateFormat = new SimpleDateFormat("dd");
			break;
		case 8:
			sDateFormat = new SimpleDateFormat("HH");
			break;
		case 9:
			sDateFormat = new SimpleDateFormat("mm");
			break;
		case 10:
			sDateFormat = new SimpleDateFormat("ss");
			break;
		case 11:
			sDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			break;
		case 12:
			sDateFormat = new SimpleDateFormat("yyyyMMdd");
			break;
		case 13:
			sDateFormat = new SimpleDateFormat("HH:mm");
			break;
		default:
			sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			break;
		}
		String date = sDateFormat.format(new Date(time));
		return date;
	}
	public static Date getCurLong(String date){
		String time=DateConvert(date.trim(), 5)+" "+dateToString(new Date(), 2);
		return stringToDate(time, 4);
	}
	public static String dateToString(Date date,int flag) {
		SimpleDateFormat sDateFormat;
		String dateString = null;
		switch (flag) {
		case 1:
			sDateFormat = new SimpleDateFormat("HH:mm");
			dateString = sDateFormat.format(date);
			break;
		case 2:
			sDateFormat = new SimpleDateFormat("HH:mm:ss");
			dateString = sDateFormat.format(date);
			break;

		case 3:
			sDateFormat = new SimpleDateFormat("YYMMDDHHmmss");
			dateString = sDateFormat.format(date);
			break;

		case 4:
			sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			dateString = sDateFormat.format(date);
			break;

		}
		return dateString;
	}

	public static String dateToAllString(Date date) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String dateString = sDateFormat.format(date);
		return dateString;
	}

	public static String dateToYYYYMM(Date date) {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = sDateFormat.format(date);
		return dateString;
	}

	public static String dateToYear() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = sDateFormat.format(new java.util.Date());
		return dateString;
	}

	public static Date stringToDate(String date, int flag) {
		SimpleDateFormat sDateFormat;
		switch (flag) {
		case 0:
			sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			try {
				return sDateFormat.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;

		case 1:
			sDateFormat = new SimpleDateFormat("HH:mm");
			try {
				return sDateFormat.parse(date);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case 2:
			sDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm");
			try {
				return sDateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		case 3:
			sDateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			try {
				return sDateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		case 4:

			sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return sDateFormat.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
